import 'dart:async';

import 'package:event_bus/event_bus.dart';

typedef EventCallback<T> = void Function(T event);

class EventBusUtil {
  //保存单例
  static final EventBusUtil _instance = EventBusUtil._internal();

  //工厂构造函数
  factory EventBusUtil() => _instance;

  //初始化eventBus
  late EventBus _eventBus;

  EventBusUtil._internal() {
    // 初始化
    _eventBus = EventBus();
  }

  /// 订阅stream列表
  // List<StreamSubscription> subscriptionList;

  /// 开启eventbus订阅 并
  StreamSubscription on<T>(EventCallback<T> callback) {
    StreamSubscription stream = _eventBus.on<T>().listen((event) {
      callback(event);
    });
    // subscriptionList.add(stream);
    return stream;
  }

  /// 发送消息
  void emit(event) {
    _eventBus.fire(event);
  }

  /// 移除steam
  void off(StreamSubscription steam) {
    steam.cancel();
  }
}

var eventBus = EventBusUtil._instance;

